package handle

import (
	"duanju-mq/db"
	"duanju-mq/pkg/formatStruct"
	"duanju-mq/pkg/log"
	"duanju-mq/pkg/util/convert"
	"encoding/json"
	"fmt"
	"net/url"
)

type mqFeedbackData struct {
	UserDeviceId string `json:"user_device_id"`
	Content      string `json:"content"`
	Ip           string `json:"ip"`
	District     string `json:"district"`
	CreateAt     int64  `json:"create_at"`
	UpdatedAt    int64  `json:"updated_at"`
	Pictures     string `json:"pictures"`
}

// 用戶反馈
func HandleAddFeedBack(rawData string) {
	info := &mqFeedbackData{}
	if err := json.Unmarshal([]byte(rawData), info); err != nil {
		log.Err(fmt.Sprintf("HandleAddFeedBack json.Unmarshal value:%v err:%+v", rawData, err))
		return
	}

	querySql := "INSERT INTO feedback(user_device_id,content,pictures,ip,district,create_at,updated_at)VALUES(?,?,?,?,?,?,?)"
	_, err := db.MqDB().Exec(querySql,
		info.UserDeviceId, info.Content, info.Pictures, info.Ip, info.District, info.CreateAt, info.UpdatedAt)
	if err != nil {
		log.Err(fmt.Sprintf("handleAddFeedBack sql:%v value:%+v err:%+v", querySql, info, err))
	}
}

type mqUserAnalyse struct {
	UserDeviceId string `json:"user_device_id" field:"user_device_id"`
	Channel      string `json:"channel" field:"channel"`
	CountryId    int64  `json:"country_id" field:"country_id"`
	OpenNum      int    `json:"open_num" field:"open_num"`
	UseDuration  int    `json:"use_duration" field:"use_duration"`
	Payment      int64  `json:"payment" field:"payment"`
	AnalyseDate  int64  `json:"analyse_date" field:"analyse_date"`
	CreateAt     int64  `json:"create_at" field:"create_at"`
	UpdatedAt    int64  `json:"updated_at" field:"updated_at"`
}

// 用户数据分析
func HandleUserAnalyse(rawData string) {

	info := &mqUserAnalyse{}
	if err := json.Unmarshal([]byte(rawData), info); err != nil {
		log.Err(fmt.Sprintf("HandleUserAnalyse json.Unmarshal value:%v err:%+v", rawData, err))
		return
	}

	var (
		parseData = formatStruct.ParseStructFields(info)

		cond = fmt.Sprintf(`ON DUPLICATE KEY UPDATE
		user_analyse.open_num=user_analyse.open_num + ?,
		user_analyse.use_duration=user_analyse.use_duration + ?,
		user_analyse.payment=user_analyse.payment + ?,
		user_analyse.updated_at=?`)
	)

	data := url.Values{}
	data.Set("user_device_id", info.UserDeviceId)
	data.Set("channel", info.Channel)
	data.Set("country_id", convert.ToString(info.CountryId))
	data.Set("open_num", convert.ToString(info.OpenNum))
	data.Set("payment", convert.ToString(info.Payment))
	data.Set("use_duration", convert.ToString(info.UseDuration))
	data.Set("analyse_date", convert.ToString(info.AnalyseDate))
	data.Set("create_at", convert.ToString(info.CreateAt))
	data.Set("updated_at", convert.ToString(info.UpdatedAt))

	querySql, values := parseData.FormatAddSql("user_analyse", data)
	querySql = fmt.Sprintf("%s %s", querySql, cond)
	values = append(values, info.OpenNum)
	values = append(values, info.UseDuration)
	values = append(values, info.Payment)
	values = append(values, info.UpdatedAt)

	if _, err := db.MqDB().Exec(querySql, values...); err != nil {
		log.Err(fmt.Sprintf("sql:%v values:%v %+v", querySql, values, err))
	}
}

type mqUserOplog struct {
	UserDeviceId  string `json:"user_device_id" field:"user_device_id"`
	OpTypeId      int    `json:"op_type_id" field:"op_type_id"`
	OpTypeName    string `json:"op_type_name" field:"op_type_name"`
	OpDescription string `json:"op_description" field:"op_description"`
	Remark        string `json:"remark" field:"remark"`
	CreateAt      int64  `json:"create_at" field:"create_at"`
	UpdatedAt     int64  `json:"updated_at" field:"updated_at"`
}

// 用户操作日志
func HandleUserOplog(rawData string) {

	info := &mqUserOplog{}
	if err := json.Unmarshal([]byte(rawData), info); err != nil {
		log.Err(fmt.Sprintf("HandleUserOplog json.Unmarshal value:%v err:%+v", rawData, err))
		return
	}

	parseData := formatStruct.ParseStructFields(info)

	data := url.Values{}
	data.Set("user_device_id", info.UserDeviceId)
	data.Set("op_type_id", convert.ToString(info.OpTypeId))
	data.Set("op_type_name", info.OpTypeName)
	data.Set("op_description", info.OpDescription)
	data.Set("remark", info.Remark)
	data.Set("create_at", convert.ToString(info.CreateAt))
	data.Set("updated_at", convert.ToString(info.UpdatedAt))

	querySql, values := parseData.FormatAddSql("user_op_log", data)
	if _, err := db.MqDB().Exec(querySql, values...); err != nil {
		log.Err(fmt.Sprintf("sql:%v values:%v %+v", querySql, values, err))
	}
}
